package com.xiaomai.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaomai.center.entity.RegisterVo;
import com.xiaomai.center.entity.UcenterMember;
import com.xiaomai.center.mapper.UcenterMemberMapper;
import com.xiaomai.center.service.UcenterMemberService;
import com.xiaomai.commonutil.JwtUtils;
import com.xiaomai.commonutil.MD5;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomai.servicebase.config.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplat;
    //验证登录
    @Override
    public String login(UcenterMember member) {
//判断账号密码是否为空
        String mobile = member.getMobile();

        if (member.getMobile()==null||member.getPassword()==null){
            throw new GuliException(20001,"验证码或者密码为空");
        }
        //查询数据库
        QueryWrapper<UcenterMember> memberwrapper = new QueryWrapper<>();
        memberwrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(memberwrapper);//根据传过来的对象查找数据库是否存在
//判断加密后的密码是否与数据库的一致
        if (ucenterMember==null||!MD5.encrypt(member.getPassword()).equals(ucenterMember.getPassword())){
            throw  new GuliException(20001,"账号或密码错误");
        }
        if (ucenterMember.getIsDeleted()){
            throw new GuliException(20001,"该账号已被禁用");
        }

        //生成token字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        System.out.println("token:"+jwtToken);
        return jwtToken;
    }

    //注册功能
    @Override
    public void register(RegisterVo registerVo) {
        String name = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        if (name==null||mobile==null||password==null||code==null){
            throw new GuliException(20001,"注册失败，请填写完整");
        }
        //判断验证码是否正确
        String redisCode = redisTemplat.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败，验证码错误");
        }
        //判断手机号码是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"注册失败,用户已存在");
        }
        //都成功则写入数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(name);
        ucenterMember.setIsDeleted(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKDRfib8wy7A2ltERKh4VygxdjVC1x5OaOb1t9hot4JNt5agwaVLdJLcD9vJCNcxkvQnlvLYIPfrZw/132");
        ucenterMember.setPassword(MD5.encrypt(password));
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember selectByWxID(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterByDay(String day) {
       return baseMapper.selectRegisterCount(day);
    }


}
